package com.scwatch.ext.service.impl;

import com.scwatch.common.orm.Limitable;
import com.scwatch.common.orm.SearchFilter;
import com.scwatch.ext.domain.LiveBroadcastAddress;
import com.scwatch.ext.repository.LiveBroadcastAddressDao;
import com.scwatch.ext.service.LiveBroadcastAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
public class LiveBroadcastAddressServiceImpl implements LiveBroadcastAddressService {

    @Override
    public Page<LiveBroadcastAddress> findAll(Map<String, String[]> params, Pageable pageable) {
        return dao.findAll(spec(params), pageable);
    }

    @Override
    public List<LiveBroadcastAddress> findAll(Map<String, String[]> params, Limitable limit) {
        return dao.findAll(spec(params), limit);
    }

    @Override
    public LiveBroadcastAddress get(Integer id) {
        return dao.findOne(id);
    }

    @Override
    @Transactional
    public LiveBroadcastAddress saveOrUpdata(LiveBroadcastAddress bean) {
        bean = dao.save(bean);
        em.flush();
        em.refresh(bean);
        return bean;
    }


    @Override
    public LiveBroadcastAddress delete(Integer id) {
        LiveBroadcastAddress entity = dao.findOne(id);
        if (entity != null) {
            dao.delete(entity);
        }
        return entity;
    }

    @Override
    public LiveBroadcastAddress[] delete(Integer[] ids) {
        LiveBroadcastAddress[] beans = new LiveBroadcastAddress[ids.length];
        for (int i = 0; i < ids.length; i++) {
            beans[i] = delete(ids[i]);
        }
        return beans;
    }

    private Specification<LiveBroadcastAddress> spec(Map<String, String[]> params) {
        Collection<SearchFilter> filters = SearchFilter.parse(params).values();
        final Specification<LiveBroadcastAddress> fsp = SearchFilter.spec(filters, LiveBroadcastAddress.class);
        Specification<LiveBroadcastAddress> sp = new Specification<LiveBroadcastAddress>() {
            public Predicate toPredicate(Root<LiveBroadcastAddress> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate pred = fsp.toPredicate(root, query, cb);
                return pred;
            }
        };
        return sp;
    }


    @Autowired
    private LiveBroadcastAddressDao dao;

    @Autowired
    private EntityManager em;

    public EntityManager getEm() {
        return em;
    }

    public void setEm(EntityManager em) {
        this.em = em;
    }
}
